#include "StandSniper.h"
#include "ShareComponents.h"
#include "RSManager.h"
#include "Box.h"
#include "Sprite.h"
#include "BulletManager.h"
#define PI 3.14

#define GUN_ANGLE 0.39225 //PI/8
#define ANGLE 1.0467 // PI/3
#define BULLET_COUNT 3
StandSniper::StandSniper(Box* box):Object(box,Obj_STATIC)
{
}


StandSniper::~StandSniper(void)
{
}



void StandSniper::init()
{
	_left_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_GUN);
	_right_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGHT_GUN);
	_left_top_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_TOP_GUN);
	_righ_top_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGH_TOP_GUN);
	_left_bottom_gun = RSManager::getInstance()->getSprite(SOLDIER_LEFT_BOTTOM_GUN);
	_right_bottom_gun = RSManager::getInstance()->getSprite(SOLDIER_RIGHT_BOTTOM_GUN);
	delay = 0;
	delay2 = 0;
	player = gl_player;
	_sprite = _left_gun;	
	_hits=1;
	_isAlive=true;
	_obj=Obj_STATIC;
	_isleft=true;
	_colBox->_v.x = 0.0f;
	_colBox->_v.y = 0.0f;
	_colBox->_position=_box->_position;	
	_colBox->_size=_box->_size;
	_colBox->_size.x /=2;
	_colBox->_size.y /=2;
	_colBox->_position.x += _colBox->_size.x / 2.0f;
	_colBox->_position.y += _colBox->_size.y / 2.0f;

}
void StandSniper::update()
{
	if(_hits <= 0)
	{
		delay2+=GAME_TIME;
		if(_isleft)
		{
			_box->_position.x+=4;
			_box->_position.y+=6;
		}
		else
		{
			_box->_position.x-=4;
			_box->_position.y+=6;
		}
		if(delay2 > 200)
		{
			delay2=0;
			_isAlive=false;				
		}
		return;
	}
	
	_angle = GetAngle();
	int index = GetIndex();
	int gunindex = SetGunIndex();
	float distance =  D3DXVec2Length(&(gl_player->_position - _box->_position));
	if (distance < 260)
	{
		delay+=GAME_TIME;
		if (delay > 500)
		{
			delay = 0;
			_sprite->Next();
			if(_bulletCount >0)
			{
				_bulletCount--;
				BulletManager::getInstance()->shoot(B_E,this->_obj,_box->_position + SetPosGun(index),gunindex*GUN_ANGLE);		
			}
			else
			{
				_bulletCount--;
				if(_bulletCount<=-1)
					_bulletCount=BULLET_COUNT;

			}

		}	
	}


}
int StandSniper::GetIndex()
{
	// moi goc hien sprite 45 do
	return (int)(_angle /ANGLE+0.5f)%6;// 2.4->2, 2.7->3
}
int StandSniper::SetGunIndex()
{
	return (int)(_angle / GUN_ANGLE+0.5f)%16;

}
float StandSniper::GetAngle()
{
	D3DXVECTOR2 des = gl_player->getCenterPos() - _box->getCenterPos();
	float alg = atan2(des.y,des.x);
	return alg < 0 ?2 * PI + alg : alg;
}
D3DXVECTOR2 StandSniper::SetPosGun(int index)
{ 
	switch (index)
	{
	case 0: _sprite = _right_gun;_isleft=false; 		
		gun_pos.x = 48;
		gun_pos.y = 50;
		break;
	case 1:	_sprite = _righ_top_gun; _isleft=false; 		
		gun_pos.x = 40;
		gun_pos.y = 78;
		break;
	case 2:	_sprite = _left_top_gun;_isleft=true; 		
		gun_pos.x = 0;
		gun_pos.y = 78;
		break;
	case 3:_sprite = _left_gun;_isleft=true;  		
		gun_pos.x = 0;
		gun_pos.y = 50;
		break;
	case 4: _sprite = _left_bottom_gun; _isleft=true; 		
		gun_pos.x = 0;
		gun_pos.y = 26;
		break;
	case 5: _sprite = _right_bottom_gun;_isleft=false; 
		gun_pos.x = 48;
		gun_pos.y = 26;
		break;
	}	
	return gun_pos; 

}
void StandSniper::render()
{	
	_sprite->Render(_box->_position);	
	Object::render();
}

Box* StandSniper::getCollisionBox()
{		
	return _colBox;
}
